﻿#region Using directives
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using gpsMe.Classes;
using gpsMe.Utils;
using gpsMe.Properties;
#endregion

namespace gpsMe.UI.Forms
{
    /// <summary>
    /// Form that allows users to change map from anyone in the filesystem which version occurs with the current
    /// </summary>
    public partial class gpsMeMapsForm : Form
    {
        #region Instance
        /// <summary>
        /// Current gpsMeMapsForm instance
        /// </summary>
        public static gpsMeMapsForm Instance { get; private set; }
        #endregion

        #region Attributes
        #endregion

        #region Constructor
        /// <summary>
        /// Default constructor of the form
        /// </summary>
        public gpsMeMapsForm()
        {
            Instance = this;
            InitializeComponent();
            this.Text = gpsMeProperties.GPSME_TITLE;
            this.SuspendLayout();
            foreach (object oFile in gpsMeOptimizer.Instance.MapsInFileSystem)
            {
                string sMapName = gpsMeMap.GetNameFromXml(oFile as string);
                if (sMapName != null)
                    this.mapsListBox.Items.Add(sMapName);
            }
            this.Resize += new EventHandler(gpsMeMapsForm_Resize);
            this.loadMapButton.Text = gpsMeLanguage.Instance.LoadMapLabel;
            this.ResumeLayout();
            if (this.mapsListBox.Items.Count > 0)
            {
                this.mapsListBox.SelectedIndex = 0;
            }
        }
        #endregion

        #region Functions / Methods / Events
        /// <summary>
        /// Invoked when the form is resized
        /// </summary>
        /// <param name="p_oSender"></param>
        /// <param name="p_oEventArgs"></param>
        private void gpsMeMapsForm_Resize(object p_oSender, EventArgs p_oEventArgs)
        {
            this.SuspendLayout();
            this.mapsListBox.Width = this.Width;
            this.mapsListBox.Height = (this.Height / 8) * 7;
            this.loadMapButton.Top = this.mapsListBox.Height;
            this.loadMapButton.Left = 0;
            this.loadMapButton.Height = this.Height - this.mapsListBox.Height;
            this.loadMapButton.Width = this.Width;
            this.ResumeLayout();
            this.Invalidate();
        }

        /// <summary>
        /// Invoked when the loadMap button is clicked
        /// </summary>
        /// <param name="p_oSender"></param>
        /// <param name="p_oEventArgs"></param>
        private void loadMapButton_Click(object p_oSender, EventArgs p_oEventArgs)
        {
            if (this.mapsListBox.SelectedIndex >= 0)
            {
                this.Enabled = false;
                Cursor.Current = Cursors.WaitCursor;
                gpsMeMap.Instance.LoadMap(gpsMeOptimizer.Instance.MapsInFileSystem[this.mapsListBox.SelectedIndex] as string);
                new gpsMeMapImageGrid();
                new gpsMeCursor();
                Cursor.Current = Cursors.Default;
                this.Enabled = true;
                this.Close();
            }
            else
            {
                MessageBox.Show(gpsMeLanguage.Instance.ErrorNoMapSelectedField);
                this.Close();
            }
        }
        #endregion
    }
}